package com.onLineCar.apply.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.onLineCar.apply.exprot.ExcelUtils;
import com.onLineCar.apply.exprot.WordExportService;
import com.onLineCar.apply.model.DriverExportRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.onLineCar.common.log.annotation.Log;
import com.onLineCar.common.log.enums.BusinessType;
import com.onLineCar.common.security.annotation.RequiresPermissions;
import com.onLineCar.apply.domain.Apply;
import com.onLineCar.apply.service.IApplyService;
import com.onLineCar.common.core.web.controller.BaseController;
import com.onLineCar.common.core.domain.Result;
import com.onLineCar.common.core.utils.poi.ExcelUtil;
import com.onLineCar.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * app更新Controller
 *
 * @author godGuang
 * @date 2023-12-24
 */
@RestController
@RequestMapping("/apply")
public class ApplyController extends BaseController
{
    @Autowired
    private IApplyService applyService;

    @Autowired
    private WordExportService wordExportService;



    /**
     * 版本更新
     */
    @GetMapping("versionUpdate")
    public Result versionUpdate(HttpServletRequest request){
        Apply apply = applyService.versionUpdate(request);
        return Result.success(apply);
    }

    /**
     * 查询app更新列表
     */
    @RequiresPermissions("apply:apply:list")
    @GetMapping("/list")
    public Result<TableDataInfo<Apply>> list(Apply apply)
    {
        startPage();
        List<Apply> list = applyService.selectApplyList(apply);
        Result<TableDataInfo<Apply>> dataTable = getDataTable(list);
        return dataTable;
    }

    /**
     * 导出app更新列表
     */
    @RequiresPermissions("apply:apply:export")
    @Log(title = "app更新", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Apply apply)
    {
        List<Apply> list = applyService.selectApplyList(apply);
        ExcelUtil<Apply> util = new ExcelUtil<Apply>(Apply.class);
        util.exportExcel(response, list, "app更新数据");
    }

    /**
     * 获取app更新详细信息
     */
    @RequiresPermissions("apply:apply:query")
    @GetMapping(value = "/{applyId}")
    public Result getInfo(@PathVariable("applyId") Long applyId)
    {
        return success(applyService.selectApplyByApplyId(applyId));
    }

    /**
     * 新增app更新
     */
    @RequiresPermissions("apply:apply:add")
    @Log(title = "app更新", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody Apply apply)
    {
        return toAjax(applyService.insertApply(apply));
    }

    /**
     * 修改app更新
     */
    @RequiresPermissions("apply:apply:edit")
    @Log(title = "app更新", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody Apply apply)
    {
        return toAjax(applyService.updateApply(apply));
    }

    /**
     * 删除app更新
     */
    @RequiresPermissions("apply:apply:remove")
    @Log(title = "app更新", businessType = BusinessType.DELETE)
	@DeleteMapping("/{applyIds}")
    public Result remove(@PathVariable Long[] applyIds)
    {
        return toAjax(applyService.deleteApplyByApplyIds(applyIds));
    }

    @GetMapping ("exportdidi")
    public void  exportdidi(HttpServletResponse response, @RequestBody DriverExportRequest driverExportRequest) throws IOException {
        try {
            wordExportService.exportWord(response,driverExportRequest);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
    //导入json格式数据
    @PostMapping("/import")
    public JSONArray importUser(@RequestPart("file") MultipartFile file) throws Exception {
        JSONArray array = ExcelUtils.readMultipartFile(file);
        System.out.println("导入数据为:" + array);
        return array;
    }

    //导入实体类数据
    @PostMapping("/import1")
    public void importUser1(@RequestPart("file") MultipartFile file) throws Exception {
        List<Apply> applies = ExcelUtils.readMultipartFile(file, Apply.class);
        System.out.println(applies);
    }


}
